<?php
/**
* //////////////////////////////////////////////////
* auto execute , for initialize config variable
* this function auto call every page has been fetch
*/
function IShopStore_permission()
{
  // Security check
  //we are allow for admin access level , see in config.php variable name ACCESS_EDIT
  if (!SecurityUtil::checkPermission('IShopStore::', '::', ACCESS_ADMIN)) {
      LogUtil::registerPermissionError(pnModUrl('Users','user','loginscreen'));
  }
}

/**
* smarty template auto call before render
*/
function _preRender(&$render){
  $lang    = FormUtil::getPassedValue ('lang', false , 'GET');
  $ctrl    = FormUtil::getPassedValue ('ctrl', false , 'GET');

  $render->assign ('_GET', $_GET);
  $render->assign ('_POST', $_POST);
  $render->assign ('_REQUEST', $_REQUEST);

  $render->assign('ctrl', $ctrl);
  $render->assign('user',   InnoUtil::getUserInfo());

  if ($lang){
    $render->assign('lang', $lang);
  }else{
    $render->assign('lang', pnUserGetLang());
  }
  $render->assign('access_edit', true);
}
/**
 * Main user function, simply return the index page.
 * @author Chayakon PONGSIRI
 * @return string HTML string
 */

function IShopStore_admin_main() {
    IShopStore_permission();
    return IShopStore_admin_page();
}
/**
* display page with out class loader
*/
function IShopStore_admin_page() {
    //IShopStore_permission();
    //$ctrl the class name
    $ctrl    = FormUtil::getPassedValue ('ctrl', 'home' , 'GET');
    //$method the method of request for edit or view enum[ view | form | delete | list | page]
    $func  = FormUtil::getPassedValue ('func', 'page' , 'GET');
    $render = pnRender::getInstance('IShopStore');
    _preRender($render);
    return $render->fetch('admin_'.$func.'_'.strtolower($ctrl).'.htm');

}
/**
* display page with class that extend Object 
*/
function IShopStore_admin_view() {
    IShopStore_permission();
    //$ctrl the class name
    $ctrl    = FormUtil::getPassedValue ('ctrl', false , 'GET');
    //$method the method of request for edit or view enum[ view | form | delete | list | page]
    $func  = FormUtil::getPassedValue ('func', 'view' , 'GET');
    //$lang enum[eng | jpn | tha]
    $lang    = FormUtil::getPassedValue ('lang', null , 'GET');
    //$id the id no if edit form
    $id      = FormUtil::getPassedValue ('id', null , 'GET');
    //pagnigation variable
    $filter  = FormUtil::getPassedValue ('filter', 0);
    $offset  = FormUtil::getPassedValue ('startnum', 0);
    $sort    = FormUtil::getPassedValue ('sort', '');
    $where   = '';

    $pagesize = pnModGetVar ('IShopStore', 'pagesize') ? pnModGetVar ('IShopStore', 'pagesize') : 20;
    $render = pnRender::getInstance('IShopStore');
    
    if ($id){
      //load class
      if (!($class = Loader::loadClassFromModule ('IShopStore', $ctrl, false)))
        return LogUtil::registerError ("Unable to load class [$ctrl] ...");

      $object  = new $class ();
      $object->get($id);
      if (method_exists($object,'selectExtendResult')){
        $resultex = $object->selectExtendResult();
        $render->assign('extendResult', $resultex);
      }
      $render->assign ('view', $object->_objData);
    }
    //_preRender($render);
    return $render->fetch('admin_'.$func.'_'.strtolower($ctrl).'.htm');
}
/**
* display page with class that extend Object Array
*/
function IShopStore_admin_form() {
    IShopStore_permission();
    //$ctrl the class name
    $ctrl    = FormUtil::getPassedValue ('ctrl', false , 'GET');
    //$method the method of request for edit or view enum[ view | form | delete | list | page]
    $func  = FormUtil::getPassedValue ('func', 'form' , 'GET');
    //$id the id no if edit form
    $id      = FormUtil::getPassedValue ('id', null , 'GET');
    //pagnigation variable
    $target  = FormUtil::getPassedValue ('target', '');
    $filter  = FormUtil::getPassedValue ('filter', 0);
    $offset  = FormUtil::getPassedValue ('startnum', 0);
    $sort    = FormUtil::getPassedValue ('sort', '');
    $where   = '';
      //var_dump($ctrl); exit;
    $pagesize = pnModGetVar ('IShopStore', 'pagesize') ? pnModGetVar ('IShopStore', 'pagesize') : 100;
    $render = pnRender::getInstance('IShopStore');
    $mode = null;

    //load class
    if (!($class = Loader::loadClassFromModule ('IShopStore', $ctrl, false)))
      return LogUtil::registerError ("Unable to load class [$ctrl] ...");

    $object  = new $class ();

    if ($id && $object){

      $object->get($id);

      $mode = 'edit';  
      $render->assign ('form', $object->_objData);
      
    }else{
      $mode = 'new';
    }
    $render->assign ('mode', $mode);
    
    if (method_exists($object,'selectExtendResult')){
      $resultex = $object->selectExtendResult();
      $render->assign('extendResult', $resultex);
    }
  
    if ($ctrl == 'about'){
        //var_dump($ctrl); exit;
      return $render->fetch('admin_'.$func.'_'.strtolower($target).'.htm');
    }

    _preRender($render);
    return $render->fetch('admin_'.$func.'_'.strtolower($ctrl).'.htm');
}
/**
* display page with class that extend Object Array
*/
function IShopStore_admin_list() {
    IShopStore_permission();
    //$ctrl the class name
    $ctrl    = FormUtil::getPassedValue ('ctrl', false , 'GET');
    //$method the method of request for edit or view enum[ view | form | delete | list | page]
    $func  = FormUtil::getPassedValue ('func', 'list' , 'GET');

    $is_export = false;

    //pagnigation variable
    $filter  = FormUtil::getPassedValue ('filter', 0);
    $offset  = FormUtil::getPassedValue ('startnum', 0);
    $sort    = FormUtil::getPassedValue ('sort', '');
    $pagesize = pnModGetVar ('IShopStore', 'pagesize') ? pnModGetVar ('IShopStore', 'pagesize') : 20;
    $render = pnRender::getInstance('IShopStore');

    //check is export
    $export = FormUtil::getPassedValue ('export', false);
    $button_export = FormUtil::getPassedValue ('button_export', false);
    $button_export_x = FormUtil::getPassedValue ('button_export_x', false);
    if ($export || $button_export || $button_export_x){
      $is_export = true;
    }
    if ($class = Loader::loadClassFromModule ('IShopStore', $ctrl, true)){
      $objectArray = new $class ();
      $where   = null;
      $sort = null;
      if (method_exists($objectArray,'genFilter')){
        $where = $objectArray->genFilter();
      }
      if (method_exists($objectArray,'genSort')){
        $sort = $objectArray->genSort();
      }
      if (empty($where)){
        $where = $objectArray->_objWhere;
      }
      if (empty($sort)){
        $sort = $objectArray->_objSort;
      }

    //Spilt page
    $pager = array();
    $pager['numitems']     = $objectArray->getCount($where , true);
    $pager['itemsperpage'] = $pagesize;
    $render->assign ('startnum', $offset);
    $render->assign ('pager', $pager);
    $objectArray->get ($where, $sort , $offset, $pagesize);

    //assign to view
    $render->assign('objectArray', $objectArray->_objData);
      if (method_exists($objectArray,'selectExtendResult')){
        $resultex = $objectArray->selectExtendResult();
        $render->assign('extendResult', $resultex);
      }
    }
    //_preRender($render);
    if (!$is_export){
      return $render->fetch('admin_'.$func.'_'.strtolower($ctrl).'.htm');
    }else{
      if (empty($mimetype)) { 
         //$mimetype = 'application/octet-stream';
         $mimetype = 'application/vnd.ms-excel';
         //$mimetype = 'text/plain';
      } 
      $result = $render->fetch('admin_export_'.strtolower($ctrl).'.htm');
      $filename = 'plankton_export_'. uniqid('@').'.csv';
      
      // Start sending headers 
      header("Pragma: public"); // required 
      header("Expires: 0"); 
      header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
      header("Cache-Control: private",false); // required for certain browsers 
      header("Content-Transfer-Encoding: binary"); 
      header("Content-Type: " . $mimetype . "; charset=windows-874" ); 
      header("Content-Length: " . strlen($result)); 
      header("Content-Disposition: attachment; filename=\"" . $filename . "\";" ); 
      
      echo $result;
      return true;
    }
}
/**
* for delete object for database by specify id
*/
function IShopStore_admin_delete() {
    //IShopStore_permission();
      //load class
    $ctrl    = FormUtil::getPassedValue ('ctrl', false , 'GET');
    $id      = FormUtil::getPassedValue ('id', false , 'GET');
    $uid     = FormUtil::getPassedValue ('uid', false , 'GET'); //icon | image | video | model
    $user    = InnoUtil::getUserInfo();

//    if ( $id && intval($user['level']) == intval(constant('ACCESS_ADMIN'))) {
      //load class
      if (!($class = Loader::loadClassFromModule ('IShopStore', $ctrl, false))){
//echo "Unable to load class [$ctrl] "; exit;
        return LogUtil::registerError ("Unable to load class [$ctrl] ...");
      }
      $object  = new $class ();
      $object->_objData[$object->_objField] = $id;
//var_dump($object);  exit;
      $object->delete();

      $list_url = pnModURL('IShopStore', 'admin', 'list' , array('ctrl'=>$ctrl));

      if (method_exists($object,'genForward')){
        $forwar_url = $object->genForward();
        pnRedirect($forwar_url);
      }else{
        pnRedirect($list_url);
      }
      die;
/*    }
    else{
     $url = pnModURL('Users', 'user', 'loginscreen' , array('ctrl'=>$ctrl) );

      pnRedirect($url);
      die;
    }*/
}

/**
* for set/unset status for hot package
* ex. index.php?module=IShopStore&func=status&ctrl=Tour&id=XXX&status=YYYY
*/
function IShopStore_admin_status() {
    IShopStore_permission();
  //get status from url 
  $id        = FormUtil::getPassedValue ('id', false , 'GET');
  $status    = FormUtil::getPassedValue ('status', false , 'GET');
  $ctrl    = FormUtil::getPassedValue ('ctrl', false , 'GET');

  if ($status && $id){
    //set status
    $where  = " itm_id = ".$id;

    $objects = array(
                    'recommend'  => $status
                   );

      DBUtil::updateObject($objects,'ishopstore_item', $where);
  }
  $list_url = pnModURL('IShopStore', 'admin', 'list' , array('ctrl'=>$ctrl));
  pnRedirect($list_url);
  die;
}

/**
* approve for payment inform
* status = 1 is Yes
*        = 2 is No
* ex. index.php?module=IShopStore&func=approve&ctrl=Inform&id=XXX
*/
function IShopStore_admin_approve() {
    IShopStore_permission();
  //get status from url 
  $id        = FormUtil::getPassedValue ('id', false , 'GET');
  $status    = FormUtil::getPassedValue ('status', false , 'GET');
  $ctrl      = FormUtil::getPassedValue ('ctrl', false , 'GET');

  if ($status && $id){
    //set status
    if($status == 2){
      $where  = " inf_id = ".$id;
      $objects = array(
                    'status'  => 1
                   );
    }else if($status == 1){
      $where  = " inf_id = ".$id;
      $objects = array(
                    'status'  => 2
                   );
    }
      DBUtil::updateObject($objects,'ishopstore_inform', $where);
  }
  $list_url = pnModURL('IShopStore', 'admin', 'list' , array('ctrl'=>$ctrl));
  pnRedirect($list_url);
  die;
}